package com.service;

import javax.servlet.http.HttpSession;

import com.hdb.DB;
import com.hdb.TGood;
import com.hdb.dao.Hib;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.util.Cart;
import org.hibernate.Session;

public class CartService extends BasicService {
    public String changeCount(int goodId, int count) {
        if (count > 100) {
            return "tooMuch";
        }
        HttpSession session = getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart == null) {
            return "error";
        }
        TGood good = db.goodDAO.findById(goodId);
        if (good.getGoodInventory() < count) {
            return "noInventory";
        }
        cart.updateCart(goodId, count);
        return "ok";
    }

    public String delGoodFromCart(int goodId) {
        HttpSession session = getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart == null) {
            return "error";
        }
        cart.goodDel(goodId);
        //session.setAttribute("cart", cart);
        return "ok";
    }


    public String clearCart() {
        HttpSession session = getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart == null) {
            return "error";
        }
        cart.getItems().clear();
        //session.setAttribute("cart", cart);
        return "ok";
    }

    @Override
    Hib getDAO() {
        return null;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }
}
